/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.employee.service;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.employee.dao.StoreEmployeeAccountingDao;
import com.chlitina.store.modules.employee.entity.StoreEmployeeAccounting;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 员工结算单Service
 * @author liujiang
 * @version 2013-11-21
 */
@Component
@Transactional(readOnly = true)
public class StoreEmployeeAccountingService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreEmployeeAccountingService.class);
	
	@Autowired
	private StoreEmployeeAccountingDao storeEmployeeAccountingDao;
	
	public StoreEmployeeAccounting get(Long id) {
		return storeEmployeeAccountingDao.findOne(id);
	}
	
	public Page<StoreEmployeeAccounting> find(Page<StoreEmployeeAccounting> page, String startDate, String endDate) {
		Date start = null;
		Date end = null;
		try {
			if (!StringUtils.isEmpty(startDate)) {
				start = DateUtils.getBeginOfDay(DateUtils.parseDate(startDate));
			}
			if (!StringUtils.isEmpty(endDate)) {
				end = DateUtils.getEndOfDay(DateUtils.parseDate(endDate));
			}
		} catch (Exception e) {
		}
		DetachedCriteria dc = storeEmployeeAccountingDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if(start != null && end != null) {
			dc.add(Restrictions.between("accounting_time", start, end));
		}
		if(start == null && end != null) {
			dc.add(Restrictions.le("accounting_time", end));
		}
		if(start != null && end == null) {
			dc.add(Restrictions.ge("accounting_time", start));
		}
		dc.addOrder(Order.desc("id"));
		return storeEmployeeAccountingDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(StoreEmployeeAccounting storeEmployeeAccounting) {
		storeEmployeeAccountingDao.save(storeEmployeeAccounting);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeEmployeeAccountingDao.delete(id);
	}
	
}
